<template>
  <el-row>
    <el-col :span="14" class="left">
      <!-- <img src="@/assets/images/logo1.jpg" alt="" /> -->
    </el-col>
    <el-col style="margin: 13% auto" :span="5" :offset="1">
      <el-form
        :model="loginForm"
        ref="loginFormRef"
        :rules="loginFormRules"
        v-if="loginForm.isLogin"
        :class="
          loginForm.isLogin
            ? 'animate__animated animate__pulse animate__duration-0.5s'
            : 'animate__animated animate__pulse animate__duration-0.5s'
        ">
        <!-- <SvgIcon name="more"></SvgIcon> -->
        <h2>登录</h2>
        <!-- <SvgIcon name="link"></SvgIcon> -->

        <el-form-item prop="username">
          <el-input
            style="height: 45px"
            v-model="loginForm.username"
            placeholder="账号"
            :prefix-icon="User"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            style="height: 45px"
            v-model="loginForm.password"
            placeholder="密码"
            type="password"
            show-password
            :prefix-icon="Lock">
          </el-input>
        </el-form-item>
        <el-form-item class="forget">
          <el-checkbox-group v-model="loginForm.remember">
            <el-checkbox label="记住我" name="type" />
          </el-checkbox-group>
          <a>忘记密码？</a>
        </el-form-item>
        <el-form-item>
          <el-button
            style="width: 100%; height: 40px"
            type="primary"
            @click="loginSubmit"
            :loading="loading"
            >登录</el-button
          >
        </el-form-item>
        <el-form-item class="register">
          <span @click="register">注册 →</span>
        </el-form-item>
      </el-form>
      <el-form
        v-else
        :model="registerForm"
        :rules="registerFormRules"
        ref="registerFormRef"
        :class="
          loginForm.isLogin
            ? 'animate__animated animate__pulse animate__duration-0.5s'
            : 'animate__animated animate__pulse animate__duration-0.5s'
        ">
        <h2>注册</h2>
        <el-form-item prop="username">
          <el-input
            style="height: 45px"
            v-model="registerForm.username"
            placeholder="账号"
            :prefix-icon="User"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            style="height: 45px"
            v-model="registerForm.password"
            placeholder="密码"
            type="password"
            show-password
            :prefix-icon="Lock"></el-input>
        </el-form-item>
        <el-form-item prop="repassword">
          <el-input
            style="height: 45px"
            v-model="registerForm.repassword"
            placeholder="确认密码"
            type="password"
            show-password
            :prefix-icon="Lock"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            style="width: 100%; height: 40px"
            type="primary"
            @click="registerSubmit"
            >提交</el-button
          >
        </el-form-item>
        <el-form-item class="register">
          <span @click="register">← 返回</span>
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>
</template>
<script setup>
import { reactive, ref } from 'vue';
import { User, Lock, View, Hide } from '@element-plus/icons-vue';
import { registerApi, loginApi } from '@/api/user.js';
import ElementPlus from 'element-plus';
import { useUserStore } from '@/stores/user.js';
import { useRouter } from 'vue-router';
import SvgIcon from '../../components/SvgIcon.vue';
const loading = ref(false);
// 登录数据
const loginForm = reactive({
  username: 'jggjgg',
  password: '123456',
  remember: [],
  isLogin: true
});
const loginFormRef = ref(null);
const loginFormRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 5, max: 10, message: '长度在 5 到 10 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    {
      pattern: /^\S{6,15}/,
      message: '请输入6-15位的非空字符',
      trigger: 'blur'
    }
  ]
};
const validateRePass = (rule, value, cb) => {
  if (value !== registerForm.password) {
    cb(new Error('两次密码不一致'));
  } else {
    cb();
  }
};
const registerFormRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 5, max: 10, message: '长度在 5 到 10 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    {
      pattern: /^\S{6,15}/,
      message: '请输入6-15位的非空字符',
      trigger: 'blur'
    }
  ],
  repassword: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    {
      pattern: /^\S{6,15}/,
      message: '请输入6-15位的非空字符',
      trigger: 'blur'
    },
    { validator: validateRePass, trigger: 'blur' }
    // {
    //   validator: (rule, value, cb) => {
    //     if (value !== registerForm.password) {
    //       cb(new Error('两次密码不一致'));
    //     } else {
    //       cb();
    //     }
    //   },
    //   trigger: 'blur'
    // }
  ]
};
// 注册数据
const registerForm = reactive({
  username: '',
  password: '',
  repassword: '',
  isLogin: true
});
const registerFormRef = ref(null);
function register() {
  loginForm.isLogin = !loginForm.isLogin;
}
const userStore = useUserStore();
const router = useRouter();
const loginSubmit = async () => {
  loginFormRef.value.validate(async (valid, fields) => {
    if (valid) {
      loading.value = true;
      const res = await loginApi(loginForm);
      console.log(res, 'res');
      loading.value = false;
      if (res.code == 0) {
        ElMessage.success('登录成功');
        userStore.setToken(res.token);
        loginFormRef.value.resetFields();
        router.replace('/');
      } else {
        ElMessage.error(res.message);
      }
    } else {
      loading.value = false;
    }
  });
};
const registerSubmit = async () => {
  registerFormRef.value.validate(async (valid, fields) => {
    if (valid) {
      const res = await registerApi(registerForm);
      if (res.code == 0) {
        ElMessage.success('注册成功');
        registerFormRef.value.resetFields();

        loginForm.isLogin = !loginForm.isLogin;
      } else {
        ElMessage.error(res.message);
      }
    } else {
      console.log('error submit!', fields);
    }
  });
};
</script>
<style lang="less" scoped>
:deep(.el-form-item__content) {
  display: flex;
  align-items: center;
  justify-content: space-between !important;
}
.left {
  height: 100vh;
  width: 100vw;
  border-radius: 0 20px 20px 0;
  background: url('../../assets/images/login-bgc.jpg') center center/cover
    no-repeat;
  box-sizing: border-box;
}
.forget {
  display: flex;
  align-items: center;
  justify-content: space-between;
  color: #409eff;
}
.register {
  cursor: pointer;
  color: #409eff;
}
</style>
